package cn.swanit.iot;

import cn.swanit.iot.verticles.HttpServerVerticle;
import cn.swanit.iot.verticles.MqttServerVerticle;
import cn.swanit.iot.verticles.TcpServerVerticle;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.impl.cpu.CpuCoreSensor;
import io.vertx.core.json.JsonObject;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class App {

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx(new VertxOptions()
                .setEventLoopPoolSize(CpuCoreSensor.availableProcessors()));

        int mqttPort = 1883;
        JsonObject mqttConfig = new JsonObject().put("port", mqttPort).put("host", "0.0.0.0");
        DeploymentOptions mqttOptions = new DeploymentOptions().setConfig(mqttConfig);
        vertx.deployVerticle(new MqttServerVerticle(), mqttOptions)
                .onSuccess(id -> {
                    log.info("MQTT服务启动成功 {} {}", mqttPort, id);
                }).onFailure(throwable -> {
                    log.error("MQTT服务启动失败, {}", throwable.getMessage());
                });

        int tcpPort = 8190;
        JsonObject tcpConfig = new JsonObject().put("port", tcpPort).put("host", "0.0.0.0");
        DeploymentOptions tcpOptions = new DeploymentOptions().setConfig(tcpConfig);
        vertx.deployVerticle(new TcpServerVerticle(), tcpOptions)
                .onSuccess(id -> {
                    log.info("TCP服务启动成功 {} {}", tcpPort, id);
                }).onFailure(throwable -> {
                    log.error("TCP服务启动失败, {}", throwable.getMessage());
                });

        int httpPort = 8090;
        JsonObject httpConfig = new JsonObject().put("port", httpPort).put("host", "0.0.0.0");
        DeploymentOptions httpOptions = new DeploymentOptions().setConfig(httpConfig);
        vertx.deployVerticle(new HttpServerVerticle(), httpOptions)
                .onSuccess(id -> {
                    log.info("HTTP服务启动成功 {} {}", httpPort, id);
                }).onFailure(throwable -> {
                    log.error("HTTP服务启动失败, {}", throwable.getMessage());
                });
    }
}
